package cn.fl.system.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.flow.service.IFlowService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysFlowTaskHService;
import cn.fl.system.vo.SysFlowTaskHVO;
import fl.flow.facade.flow.dto.FlowCompleteParamsDTO;

/**
 * @Description: 流程任务节点操作历史 控制层
 */
@Controller
@RequestMapping("/system/sysflowtaskh")
public class SysFlowTaskHController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysFlowTaskHController.class);

	/**
	 * SysFlowTaskH 服务层BEAN
	 */
	@Autowired
	private ISysFlowTaskHService sysFlowTaskHService;
	
	/**
	 * 流程服务
	 */
	@Autowired
	private IFlowService flowService;

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String taskDefKey = request.getParameter("taskDefKey");
		String taskId = request.getParameter("taskId");
		String processDefinitionKey = request.getParameter("processDefinitionKey");
		String processDefinitionId = request.getParameter("processDefinitionId");
		String attchmnt = request.getParameter("attchmnt");
		String taskOpinion = request.getParameter("taskOpinion");
		String businessKey = request.getParameter("businessKey");
		String processInstanceId = request.getParameter("processInstanceId");
		String businessType = request.getParameter("businessType");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		
		if(StringUtils.isEmpty(taskDefKey) 
				|| StringUtils.isEmpty(taskId) 
				|| StringUtils.isEmpty(processDefinitionKey)
				|| StringUtils.isEmpty(processDefinitionId) 
				|| StringUtils.isEmpty(businessKey)
				|| !StringUtils.isNumeric(businessKey)//验证Long类型
				|| StringUtils.isEmpty(processInstanceId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("流程节点参数不完整！");
			return result;
		}
		
		try {
			SysFlowTaskHVO entity = new SysFlowTaskHVO();
			entity.setCrtTm(new Date());
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			entity.setBusinessKey(Long.valueOf(businessKey));
			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			entity.setProcDefId(processDefinitionId);
			entity.setProcDefKey(processDefinitionKey);
			entity.setProcInstId(processInstanceId);
			entity.setSysCd(CurrentThreadContext.getCurrentSysCd());
			entity.setTaskDefKey(taskDefKey);
			entity.setTaskId(taskId);
			entity.setTaskOpinion(taskOpinion);
			entity.setBusinessType(businessType);
			
			DataResultDTO dataResultDTO = this.sysFlowTaskHService.add(entity);
			if(null == dataResultDTO || !dataResultDTO.getCode().equals(BaseExceptionConstant.BASE_EXCEPTION_CODE_200)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("操作失败！");
				return result;
			}else{
				//加入附件,这里要容错,如果附件错误则忽略
				if(StringUtils.isNotEmpty(attchmnt)){
					Long hId = dataResultDTO.getId();
					if(StringUtils.isNotEmpty(hId)){
						FlowCompleteParamsDTO flowCompleteParamsDTO = new FlowCompleteParamsDTO();
						flowCompleteParamsDTO.setProcInstId(processInstanceId);
						flowCompleteParamsDTO.setTaskId(taskId);
						this.flowService.addFlowAttchmnt(hId, attchmnt, flowCompleteParamsDTO);
					}
				}
			}

		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("操作失败!");
			logger.error("", ex);
		}

		return result;
	}
	
	/**
	 * 新增-只保存意见和附件到本地,和流程引擎无交互
	 */
	@RequestMapping(value = "/addLocal.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addLocal(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String taskDefKey = request.getParameter("taskDefKey");
		String taskId = request.getParameter("taskId");
		String processDefinitionKey = request.getParameter("processDefinitionKey");
		String processDefinitionId = request.getParameter("processDefinitionId");
		String attchmnt = request.getParameter("attchmnt");
		String taskOpinion = request.getParameter("taskOpinion");
		String businessKey = request.getParameter("businessKey");
		String processInstanceId = request.getParameter("processInstanceId");
		String businessType = request.getParameter("businessType");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		
		if(StringUtils.isEmpty(taskDefKey) 
				|| StringUtils.isEmpty(taskId) 
				|| StringUtils.isEmpty(processDefinitionKey)
				|| StringUtils.isEmpty(processDefinitionId) 
				|| StringUtils.isEmpty(businessKey)
				|| !StringUtils.isNumeric(businessKey)//验证Long类型
				|| StringUtils.isEmpty(processInstanceId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("流程节点参数不完整！");
			BaseLogUtils.info(logger, "addLocal", "请求参数:" + this.getRequestParameter(request));
			return result;
		}
		
		try {
			SysFlowTaskHVO entity = new SysFlowTaskHVO();
			entity.setCrtTm(new Date());
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			entity.setBusinessKey(Long.valueOf(businessKey));
			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			entity.setProcDefId(processDefinitionId);
			entity.setProcDefKey(processDefinitionKey);
			entity.setProcInstId(processInstanceId);
			entity.setSysCd(CurrentThreadContext.getCurrentSysCd());
			entity.setTaskDefKey(taskDefKey);
			entity.setTaskId(taskId);
			entity.setTaskOpinion(taskOpinion);
			entity.setBusinessType(businessType);
			
			DataResultDTO dataResultDTO = this.sysFlowTaskHService.addLocal(entity);
			if(null == dataResultDTO || !dataResultDTO.getCode().equals(BaseExceptionConstant.BASE_EXCEPTION_CODE_200)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("操作失败！");
				return result;
			}else{
				//加入附件,这里要容错,如果附件错误则忽略
				if(StringUtils.isNotEmpty(attchmnt)){
					Long hId = dataResultDTO.getId();
					if(StringUtils.isNotEmpty(hId)){
						FlowCompleteParamsDTO flowCompleteParamsDTO = new FlowCompleteParamsDTO();
						flowCompleteParamsDTO.setProcInstId(processInstanceId);
						flowCompleteParamsDTO.setTaskId(taskId);
						this.flowService.addFlowAttchmnt(hId, attchmnt, flowCompleteParamsDTO);
					}
				}
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("操作失败!");
			logger.error("", ex);
		}

		return result;
	}
}

